#!/usr/bin/env bash
# 在deploy机器做其他node的ssh免密操作,为后续ansible自动部署node节点做准备
ssh_secret_free(){
  local rootpasswd=$1 netnum=$2 nethosts=$3
#为master节点主机部署ssh免密操作
for host in `echo "${nethosts}"`
do
    echo -e "\033[31m ================== ${netnum}.${host} ==================\033[0m";
    if [[ ${USER} == 'root' ]];then
        [ ! -f /${USER}/.ssh/id_rsa ] &&\
        ssh-keygen -t rsa -P '' -f /${USER}/.ssh/id_rsa;yum -y install sshpass
    else
        [ ! -f /home/${USER}/.ssh/id_rsa ] &&\
        ssh-keygen -t rsa -P '' -f /home/${USER}/.ssh/id_rsa
    fi
    sshpass -p ${rootpasswd} ssh-copy-id -o StrictHostKeyChecking=no ${USER}@${netnum}.${host}
    if cat /etc/redhat-release &>/dev/null;then
        ssh -o StrictHostKeyChecking=no ${USER}@${netnum}.${host} "echo -e  '\033[32m SSH Remote Key deployment succeeded\033[0m'"
    else
        echo -e  '\033[32m RedHat series is currently deployed in the system. Please confirm the system version \033[0m'
        exit 0
    fi
done
}
#设置master系统环境准备
Environmental(){
#关闭防火墙firewalld
local name=$1
  hostnamectl set-hostname $name
  a=`hostname`
    echo "当前主机名：${a}"
  systemctl stop firewalld
  systemctl disable firewalld
 #关闭selinux
  setenforce 0
  sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config
  #临时关闭交换分区
  swapoff  -a
  sed -i '/swap/d' fstab
  #临时设置DNS
  #echo "nameserver 114.114.114.114" >>/etc/resolv.conf
  b=`cat /etc/redhat-release`
  echo "操作系统版本为：${b}"
  #同步时区
  timedatectl set-timezone Asia/Shanghai
  #添加yum源
  mkdir /etc/yum.repos.d/bak
  mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
  curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
}
Kernel(){
#配置内核参数，将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
modprobe br_netfilter
sysctl --system
}
ipaddr=`ifconfig -a |grep -v docker| grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}' |grep -v 172`
docker_install(){
  local bashpath="$(cd `dirname $0`;pwd)"
if [ -e ${bashpath}/file/docker-ce.tar.gz ];then
    tar -xvf ${bashpath}/file/docker-ce.tar.gz
    cd ${bashpath}/file/docker
    [ -f ${bashpath}/file/docker/docker-ce.repo ] && cp docker-ce.repo /etc/yum.repos.d/docker-ce.repo || \
    echo -e "\033[31m 【`hostname` ERROR not find ${bashpath}/file/docker/docker-ce.repo 】";exit 0
    yum -y install docker-ce
    systemctl enable docker --now
else
yum install -y vim bash-completion net-tools gcc ipvsadm ipset bc
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
yum -y install docker-ce
systemctl enable  docker
systemctl start docker
fi
#开启路由转发
cat >>/etc/sysctl.conf<<EOF
net.ipv4.ip_forward = 1
EOF
sysctl -p
}
yum_install(){
  yum install -y vim bash-completion net-tools gcc ipvsadm ipset bc
#使用aliyun源安装docker-ce
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce-19.03
#注：yum-config-manager命令配置aliyun源，但是这个命令来源于yum-utils，所以需要先安装yum-utils
#安装完docker后添加aliyun的docker仓库加速器
}
reload(){
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["https://fl791z1h.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
}

#help帮助信息
help(){
  echo -e  '\033[32m 各个节点需要在同一个局域网内，且配置了/etc/hosts文件 \033[0m'
  echo -e  '\033[32m 当前网络不可用，请检查网络连接状态 \033[0m'
  echo -e  '\033[32m 可以使用：ping -c 3 www.baidu.com \033[0m'
  echo -e  '\033[32m 通过：nmcli connection show，查看网卡的状态  \033[0m'
  echo -e  '\033[32m 可以使用cat /etc/sysconfig/network-scripts/ifcfg-ens33，查看网卡的配置（ens33根据当前正在使用的网卡名称一致即可）  \033[0m'

}


usage(){
echo -e "\033[31m Example: bash run.sh 密码 192.168.1 51\ 52\ 53\033[0m"
echo -e "\033[31m Example: bash init.sh 密码 netnum nethosts name  \033[0m"
echo -e "\033[31m 使用kubeadm方式安装k8s_1.18(kubeadm)/二进制安装k8s(Binary) \033[0m"
echo -e "\033[31m 建议使用kubeadm方式安装k8s_1.18(kubeadm)\033[0m"
}